/*
 *
 * Enter 1 to sort in ascending order,
 * Enter 2 to sort in descending order: 1
 * Data items in original order
 * 2 6 4 8 10 12 89 68 45 37
 * Data items in ascending order
 * 2 4 6 8 10 12 37 45 68 89
 *
 *
 */

#include <iostream>
#include <iomanip>

void selectSorted (int [], const int, bool (*) ( int, int ) );
void swap ( int * const, int * const);
bool ascending ( int, int);
bool descending ( int, int);

int main ( int argc, const char* argv[]){

	const int arraySize = 10;
	int order = 0;
	int counter;
	int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

	std::cout << "Enter 1 to sort in ascending order,\n"
			  << "Enter 2 to sort in descending order: ";

	std::cin >> order;

	std::cout << "Original order \n";

	for ( counter = 0; counter < arraySize; ++counter){
		std::cout << std::setw ( 4 ) << a[ counter ];
	}
	std::cout << std::endl;

	if ( order == 1 )
	{
		selectSorted(a, arraySize, ascending);
	}
	else
		selectSorted( a, arraySize, descending);

	for ( counter = 0; counter < arraySize; ++counter){
		std::cout << std::setw ( 4 ) << a[ counter ];
	}
	std::cout << "\n Modify order \n";
	std::cout << std::endl;
}

void selectSorted ( int work[], const int size, bool (*compare)( int, int ) ){
	int smallestOrLargest;

	for ( int i = 0; i < size - 1; ++i){
		smallestOrLargest = i;
		for ( int index = i + 1; index < size; ++index){
			if ( !(*compare)( work[ smallestOrLargest ],  work[ index ])){
			smallestOrLargest = index;}
		}
		swap( &work[ smallestOrLargest], &work[ i ]);
	}
}

void swap ( int * const elemnt1Ptr, int * const element2Ptr ){

	int hold = *elemnt1Ptr;
	*elemnt1Ptr = *element2Ptr;
	*element2Ptr = hold;

}

bool ascending (int a, int b){
	return a < b;
}
bool descending (int a, int b){
	return a > b;
}
